package com.gengzp.system.controller;

import com.gengzp.common.annotations.OperationLogIgnore;
import com.gengzp.common.enums.IterationStatusEnum;
import com.gengzp.common.model.generic.req.SearchByIdReq;
import com.gengzp.common.model.generic.resp.Result;
import com.gengzp.common.model.pagination.resp.PaginationResponse;
import com.gengzp.common.model.versionIteration.req.ChangeVersionIterationStatusReq;
import com.gengzp.common.model.versionIteration.req.CreateVersionIterationReq;
import com.gengzp.common.model.versionIteration.req.EditVersionIterationReq;
import com.gengzp.common.model.versionIteration.req.PageVersionIterationReq;
import com.gengzp.common.model.versionIteration.resp.PageVersionIterationResp;
import com.gengzp.common.model.versionIteration.resp.SearchVersionIterationResp;
import com.gengzp.common.utils.DistributedLockUtils;
import com.gengzp.permission.annotation.AuthIgnore;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.system.service.VersionIterationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName VersionIterationController
 * @Description 迭代管理控制层
 * @Author gengzp
 * @Date 2025/10/11 15:46
 */
@RestController
@RequestMapping("/version_iteration")
@Api(tags = "迭代管理模块")
public class VersionIterationController {

    @Autowired
    private VersionIterationService versionIterationService;

    /**
     * 版本迭代操作分布式锁key前缀
     */
    private static final String VERSION_ITERATION_OPERATION_LOCK_KEY_PREFIX = "version_iteration_operation_lock:";

    /**
     * 创建版本迭代信息
     */
    @PostMapping("/create")
    @ApiOperation("创建版本迭代信息")
    @CheckPermission("version_iteration:write:create")
    public Result<Void> createVersionIteration(@Valid @RequestBody CreateVersionIterationReq req) {
        versionIterationService.createVersionIteration(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑版本迭代信息
     */
    @PostMapping("/edit_by_id")
    @ApiOperation("编辑版本迭代信息")
    @CheckPermission("version_iteration:write:edit")
    public Result<Void> editVersionIterationById(@Valid @RequestBody EditVersionIterationReq req) {
        // 通过分布式锁管控迭代的写操作
        DistributedLockUtils.doExecuteWithLock(() ->
                        versionIterationService.editVersionIterationById(req),
                VERSION_ITERATION_OPERATION_LOCK_KEY_PREFIX + req.getId());
        return Result.getSuccessResult();
    }

    /**
     * 变更迭代流程
     */
    @PostMapping("/change_status")
    @ApiOperation("变更迭代流程")
    @CheckPermission("version_iteration:write:change_status")
    public Result<Void> changeVersionIterationStatusById(@Valid @RequestBody ChangeVersionIterationStatusReq req) {
        // 防止枚举值不关联
        IterationStatusEnum.getValueByCode(req.getStatus());
        // 通过分布式锁管控迭代的写操作
        DistributedLockUtils.doExecuteWithLock(() ->
                        versionIterationService.changeVersionIterationStatusById(req),
                VERSION_ITERATION_OPERATION_LOCK_KEY_PREFIX + req.getId());
        return Result.getSuccessResult();
    }

    /**
     * 删除迭代
     */
    @PostMapping("/remove_by_id")
    @ApiOperation("删除迭代")
    @CheckPermission("version_iteration:write:remove")
    public Result<Void> removeVersionIterationById(@Valid @RequestBody SearchByIdReq req) {
        // 通过分布式锁管控迭代的写操作
        DistributedLockUtils.doExecuteWithLock(() ->
                        versionIterationService.removeVersionIterationById(Long.valueOf(req.getId())),
                VERSION_ITERATION_OPERATION_LOCK_KEY_PREFIX + req.getId());
        return Result.getSuccessResult();
    }

    /**
     * 根据id查询版本迭代详情
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/search_by_id")
    @ApiOperation("根据id查询版本迭代详情")
    @CheckPermission("version_iteration:read")
    public Result<SearchVersionIterationResp> searchVersionIterationById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(versionIterationService.searchVersionIterationById(Long.valueOf(req.getId())));
    }

    /**
     * 分页查询版本迭代
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/page")
    @ApiOperation("分页查询版本迭代")
    public Result<PaginationResponse<PageVersionIterationResp>> pageQueryVersionIterationResp(@Valid @RequestBody PageVersionIterationReq req) {
        return Result.getSuccessResult(versionIterationService.pageQueryVersionIterationResp(req));
    }

}
